/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.common.weibolib;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.regex.Matcher;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.v4.content.LocalBroadcastManager;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.style.ImageSpan;
import android.text.style.URLSpan;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.View.OnTouchListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.common.allCommon.CommonUtils;
import com.common.allCommon.RegexUtils;
import com.common.android.utils.smiley.Smiley;
import com.common.android.utils.smiley.Smileyhelper;
import com.product.android.utils.wbAtUtils.WbAtView;
import com.starappallcommon.R;
/**
 * Class containing some static utility methods.
 */
public class Utils {

	public static final int SMILEY_SIZE = 18;

	private Utils() {
	};

	public static final int MAX_SIZE_OF_TWEET = 140;
	public static final int TYPE_ND_TWEET = 0;
	public static final int TYPE_SINA_TWEET = 1;
	public static final int TYPE_UNKNOWN_TWEET = -1;

	/**
	 * 解析表情
	 * 
	 * @param context
	 * @param sourceContent
	 * @return
	 */
	public static SpannableString resolveSmiley(Context context,
			String sourceContent) {
		return Smileyhelper.getInstance().getSmileySpannableString(
				sourceContent, Smiley.TYPE_WEIBO, SMILEY_SIZE);
	}

	public static SpannableString resolveHashtags(String sourceContent,
			SpannableString ss) {
		if (ss == null) {
			ss = new SpannableString(sourceContent);
		}
		Matcher m = RegexUtils.HASHTAGS_PATTERN.matcher(sourceContent);
		if (m.find()) { // 只解析第一个tag
			String hashtagsMatch = m.group();
			String subHashtagsMatch = hashtagsMatch.substring(0,
					hashtagsMatch.length());
			ss.setSpan(new URLSpanNoUnderline(subHashtagsMatch,
					URLSpanNoUnderline.SpanType.HASHTAGS), m.start(), m.end(),
					SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
		}
		return ss;
	}


	
	/**
	 * 用于生日提醒那边高亮显示@XXXX
	 * 
	 * @n<b>函数名称</b> :resolveAt
	 * @brief
	 * @see
	 * @since Ver 1.1
	 * @param @param sourceContent
	 * @param @return
	 * @return SpannableString
	 * @<b>作者</b> : 黄永鑫
	 * @<b>创建时间</b> : 2013-11-19上午11:57:34
	 */
	public static SpannableString resolveAt(String sourceContent) {
		SpannableString ss = new SpannableString(sourceContent);

		Matcher m = RegexUtils.AT_PATTERN.matcher(sourceContent);
		while (m.find()) {
			ss.setSpan(new URLSpanNoUnderline(m.group()), m.start(), m.end(),
					SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
		}
		return ss;
	}

	/**
	 * 
	 * ClassName:UrlImageSpan Function: 用于与表情的ImageSpan区分 Reason: TODO ADD
	 * REASON
	 * 
	 * @author huangszh
	 * @version Utils
	 * @since Ver 1.1
	 * @Date 2013 2013-12-11 上午10:15:42
	 * 
	 * @see
	 */
	public static class UrlImageSpan extends ImageSpan {

		public UrlImageSpan(Drawable d) {
			super(d);
		}

	}

	public static class URLSpanNoUnderline extends URLSpan {
		protected SpanType mSpanType;
		protected Object mObj;

		public enum SpanType {
			URL, AT, SMILEY, HASHTAGS, UNKNOW
		}

		public URLSpanNoUnderline(String url) {
			this(url, SpanType.UNKNOW);
		}

		public URLSpanNoUnderline(String url, SpanType spanType) {
			super(url);
			mSpanType = spanType;
		}

		public URLSpanNoUnderline(String url, SpanType spanType, Object obj) {
			this(url, spanType);
			mObj = obj;
		}

		@Override
		public void updateDrawState(TextPaint ds) {
			super.updateDrawState(ds);
			ds.setUnderlineText(false);
			switch (mSpanType) {
			case HASHTAGS:
				ds.setColor(0xFFE76812);
				break;
			default:
				ds.setColor(0xFF3A7383);
				break;
			}

		}

		public SpanType getSpanType() {
			return mSpanType;
		}

		@Override
		public boolean equals(Object o) {

			if (o instanceof URLSpanNoUnderline) {
				URLSpanNoUnderline us = (URLSpanNoUnderline) o;
				if (us.mSpanType.equals(mSpanType)
						&& us.getURL().equals(getURL())) {
					return true;
				}
			}
			return false;
		}

		@Override
		public int hashCode() {
			return super.hashCode();
		}
	}

	/**
	 * 
	 * @n<b>函数名称</b>     :processLongTweet
	 * @brief 处理长微博，如果MAX_SIZE_OF_TWEET的位置处有@，url，表情，话题，则取这些字段前面的文字,
	 * 但是即使经过处理，也无法和服务端的截断完全一致，因为服务端对url长度进行了转化，而我们是完全不计算在内
	 * @see
	 * @since    Ver 1.1
	 * @param  @param longText
	 * @param  @return
	 * @return String
	 * @<b>作者</b>          :  chenqian
	 * @<b>创建时间</b>      :  2014-3-17下午4:25:53
	 */
	public static String processLongTweet(String longText) {
		if(TextUtils.isEmpty(longText)){
			return longText;
		}
		float[] len = getWordCount(longText, true);//去除了url和<M>标签
		try{
			if(len[0] <= MAX_SIZE_OF_TWEET){
				return longText;
			}
			
			StringBuilder retStr = new StringBuilder("...");
			// 防止@截断
			int atStart = findAt(longText, (int) len[1]);
			if(atStart > 0){
				return retStr.insert(0, longText.substring(0, atStart)).toString();
			}

			// 防止表情截断
			String shortText = longText.substring(0, (int) len[1]);
			int left = shortText.lastIndexOf("[");
			if(left > 0){
				String restText = longText.substring(left + 1);
				int right = restText.indexOf("]") +(left + 1) + 1;
				if(right > len[1] && right - left < 6){// 防止表情被截断，目前表情最多由5个字节组成
					return retStr.insert(0, longText.substring(0, left)).toString();
				}
			}

			// 防止url截断
			// 找出全部的url,记录下，判断MAX_SIZE_OF_TWEET是否落在其中，是的话，截取到该URL开始
			int urlStart = findUrl(longText, (int) len[1]);
			if(urlStart > 0){
				return retStr.insert(0, longText.substring(0, urlStart)).toString();
			}

			//防止话题截断
			int tagStart = findHashtag(longText, (int) len[1]);
			if(tagStart > 0){
				return retStr.insert(0, longText.substring(0, tagStart)).toString();
			}
			
			// 取结束值的最大
			return retStr.insert(0,  longText.substring(0, (int) len[1])).toString();
		}catch(StringIndexOutOfBoundsException e){
			return longText;
		}
	}

	/**
	 * 
	 * @n<b>函数名称</b>     :findAt
	 * @brief
	 * @see
	 * @since    Ver 1.1
	 * @param  @param sourceContent
	 * @param  @param end
	 * @param  @return >0 代表找到at串，<0 代表没找到,否则返回AT串的起始位置
	 * @return int
	 * @<b>作者</b>          :  chenqian
	 * @<b>创建时间</b>      :  2014-1-17下午1:54:54
	 */
	private static int findAt(String sourceContent, int end) {
		Matcher m = RegexUtils.AT_M_TAG_PATTERN.matcher(sourceContent);
		while (m.find()) {
			if (m.start() > end) {
				break;
			}
			if (m.start() < end && m.end() >= end) {
				return m.start();
			}
		}
		return -1;
	}

	/**
	 * 
	 * @n<b>函数名称</b> :findUrl
	 * @brief 长微博时，计算最大值附近的URL，避免截断，如果URL落在中间，则截取到该URL开始位置之前
	 * @see
	 * @since Ver 1.1
	 * @param @param sourceContent
	 * @param @param end
	 * @param @return
	 * @return int
	 * @<b>作者</b> : huangszh
	 * @<b>创建时间</b> : 2013-12-11下午5:15:02
	 */
	private static int findUrl(String sourceContent, int end) {
		Matcher m = RegexUtils.WEB_URL.matcher(sourceContent);
		while (m.find()) {
			if (m.start() > end) {
				break;
			}
			if (m.start() < end && m.end() >= end) {
				return m.start();
			}
		}
		return -1;
	}
	

	/**
	 * 
	 * @n<b>函数名称</b>     :findHashtag
	 * @brief 微博时，计算最大值附近的话题，避免截断，如果话题落在中间，则截取到该话题位置之前
	 * @see
	 * @since    Ver 1.1
	 * @param  @param sourceContent
	 * @param  @param end
	 * @param  @return
	 * @return int
	 * @<b>作者</b>          :  chenqian
	 * @<b>创建时间</b>      :  2014-3-17下午4:30:16
	 */
	private static int findHashtag(String sourceContent, int end){
		Matcher m = RegexUtils.HASHTAGS_PATTERN.matcher(sourceContent);
		while (m.find()) {
			if (m.start() > end) {
				break;
			}
			if (m.start() < end && m.end() >= end) {
				return m.start();
			}
		}
		return -1;
	}

	/**
	 * 
	 * @n<b>函数名称</b> :getNotUrlString
	 * @brief 长微博时，计算非URL的字符串长度
	 * @see
	 * @since Ver 1.1
	 * @param @param str
	 * @param @param sb
	 * @param @return
	 * @return int
	 * @<b>作者</b> : chenqian
	 * @<b>创建时间</b> : 2014-1-14下午5:15:02
	 */
	private static String getNotUrlString(String str) {
		int start = 0;
		int end = 0;
		StringBuilder sb = new StringBuilder();                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
		Matcher m = RegexUtils.WEB_URL.matcher(str);
		//加入开头的非URL串
		if(m.find()){
			start = m.start();
			end = m.end();
			if(start > 0){
				sb.append(str.substring(0, start));
			}
		}
		//加入中间的非URL串
		while (m.find()) {
			start = m.start();
			if(end < start){
				sb.append(str.substring(end, start));		
			}
			end = m.end();					
		}
		//加入结尾的非URL串
		if(end < str.length()){
			sb.append(str.substring(end));		
		}
		return sb.toString();
	}

	/**
	 * 
	 * @n<b>函数名称</b>     :getWordCount
	 * @brief 获得微博字数，汉字算1个，半角英文数字符号等算0.5个
	 * @see
	 * @since    Ver 1.1
	 * @param  sourceStr 原微博内容
	 * @param  flag 是否计算剔除URL和<M></M>标签,flag = false的情况下， float[1]无意义
	 * @param  @return
	 * @return float[]
	 *               float[0]是字符串的长度
	 *               float[1]是字符串的长度到达140时的实际文字数，这个文字数是不论汉字英文数字都算1个的,而且没有提出url和M标签
	 * @<b>作者</b>          :  chenqian
	 * @<b>创建时间</b>      :  2014-1-13下午2:48:18
	 */
	public static float[] getWordCount(String s, boolean flag) {
		
		return getWordCount(s, flag, MAX_SIZE_OF_TWEET);

	}
	
	public static float[] getWordCount(final String sourceStr, final boolean flag,final int maxLength) {
		String processStr = sourceStr;
		if(flag){
			processStr = getNotUrlString(processStr);
			processStr = WbAtView.getOnlyAtContent(processStr);//剔除<M XXX></M>
		}
		//sourceStr:未处理的字符串
		//processStr:处理的字符串
		//len[0]:按照汉字为1，其他为0.5的规则，计算processStr的长度
		//len[1]:计算processStr的长度,直到len[0]长度超过MAX_SIZE_OF_TWEET	
		float len[] = new float[2];
		int ascii1,ascii2;
		int j = 0;
		for (int i = 0; i < processStr.length(); ++i) {
			//ascii1:取处理串的每个ascii码
			ascii1 = Character.codePointAt(processStr, i);
			if (ascii1 >= 0 && ascii1 <= 255){//如果在ascii码范围内，则计算长度为0.5
				len[0] += 0.5;
			}else{
				++len[0];//否则计算长度为1
			}
			
			if(flag && len[0] <= maxLength){
				//ascii2:取原串的每个ascii码
				ascii2 = Character.codePointAt(sourceStr, j);
				
				//如果原串的ascii码和处理串的ascii码不相等，则后移取原串取下一个ascii码,直到相等
				while(ascii1 != ascii2 && j < sourceStr.length()){
					++j;
					ascii2 = Character.codePointAt(sourceStr, j);
				}
				//如果原串的ascii码和处理串的ascii码相等，则原串和处理串都向后移动一位继续比较
				if(ascii1 == ascii2 && j < sourceStr.length()){
					++j;
				}
			}
		}
		len[1] = j;
		return len;
	}
	
	/**
	 * 
	 * @n<b>函数名称</b>     :getMsgContentCount
	 * @brief	计算鲜花留言的字数，汉字算2；字母，数字等单字节算1
	 * @see
	 * @since    Ver 1.1
	 * @param  @param str
	 * @param  @return
	 * @return int
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-3-18上午9:26:38
	 */
	public static int getMsgContentCount(String str) {
		int length = 0;
		for (int i = 0; i < str.length(); i++) {
			int ascii = Character.codePointAt(str, i);
			if (ascii >= 0 && ascii <= 255){
				length += 1;
			}else{
				length += 2;
			}
		}
		return length;
	}
	
	public static class ShowInputTouchListener implements OnTouchListener{
		private float down_x = 0;
		private float down_y = 0;
		private float up_x = 0;
		private float up_y = 0;
		private final static int LIMIT = 20;
		private EditText editText;
		private Context context;
		private LinearLayout mSmileyLayout;
		public ShowInputTouchListener(Context ctx, EditText edit, LinearLayout smileyLayout){
			editText = edit;
			context = ctx;
			mSmileyLayout = smileyLayout;
		}

		/**
		 * @n<b>函数名称</b>     :onTouch
		 * @brief
		 * @see
		 * @since    Ver 1.1
		 * @param  @param v
		 * @param  @param event
		 * @param  @return
		 * @<b>作者</b>          :  Administrator
		 * @<b>创建时间</b>      :  2014-1-24下午5:26:27      
		*/
		
		@Override
		public boolean onTouch(View v, MotionEvent event) {
			switch(event.getAction()){
			case MotionEvent.ACTION_DOWN:
				down_x = event.getX();
				down_y = event.getY();
				up_x = down_x;
				up_y = down_y;
				break;
			case MotionEvent.ACTION_MOVE:
				float x = event.getX();
				if(Math.abs(x - down_x) > Math.abs(up_x - down_x)){
					up_x = x;
				}
				float y = event.getY();
				if(Math.abs(y - down_y) > Math.abs(up_y - down_y)){
					up_y = y;
				}
				break;
			case MotionEvent.ACTION_UP:
				if(Math.abs(up_x - down_x) < LIMIT || Math.abs(up_y - down_y) < LIMIT){
					CommonUtils.showSoftInputMethod(context, editText);
					if(null != mSmileyLayout && mSmileyLayout.isShown()){
						mSmileyLayout.setVisibility(View.GONE);
					}
				}
			}
			return false;
		}		
	}
	
	
	/**
	 * 
	 * @n<b>函数名称</b>     :findInMTag
	 * @brief	判断@的人名是否已经加过M标签了
	 * @see
	 * @since    Ver 1.1
	 * @param  @param begin
	 * @param  @param end
	 * @param  @param indexs
	 * @param  @param currentIndex
	 * @param  @return
	 * @return int
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-3-18下午2:19:08
	 */
	private static int findInMTag(int begin,int end,ArrayList<Point> indexs,int currentIndex){
		int result = -1;
		int size = indexs == null ? 0 : indexs.size();
		if(size == 0){
			return result;
		}
		Point p;
		for(; currentIndex < size; currentIndex++){
			p = indexs.get(currentIndex);
			// 如果落在中间，说明已经加上M标签了
			if(begin > p.x && end < p.y){
				return currentIndex + 1;// 当前位置找到，下次查找位置需要+1
			}
		}
		return  result;
	}
	
	/**
	 * 
	 * @n<b>函数名称</b> :setUrlImageSpan
	 * @brief 用于微博短链接背景图片切换效果，重新设置ImageSpan
	 * @see
	 * @since Ver 1.1
	 * @param @param widget
	 * @param @param imagespan
	 * @param @param buffer
	 * @param @param press
	 * @param @param content
	 * @param @return
	 * @return ImageSpan
	 * @<b>作者</b> : huangszh
	 * @<b>创建时间</b> : 2013-12-10下午5:13:05
	 */
	public static UrlImageSpan setUrlImageSpan(TextView widget,
			UrlImageSpan imagespan, Spannable buffer, boolean press,
			String content) {
		int start = buffer.getSpanStart(imagespan);
		int end = buffer.getSpanEnd(imagespan);
		buffer.removeSpan(imagespan);
		// 找不到设置span的地方，直接返回null
		if(start == -1 || end == -1) {
		    return null;
		}
		imagespan = new UrlImageSpan(getImageSpanDrawble(widget.getContext(),
				press, content));
		buffer.setSpan(imagespan, start, end,
				SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
		widget.setText(buffer);
		return imagespan;
	}
	

	/**
	 * 
	 * @n<b>函数名称</b> :getImageSpanDrawble
	 * @brief 创建带文字和背景的Drawable,文字可以由外部传入，目前传null默认显示网页链接。
	 *        另暂时只考虑微博短链接替换使用，其他地方使用可以进一步修改，由外部传入不同状态时的图片，图片大小，文字大小等等属性
	 * @see
	 * @since Ver 1.1
	 * @param @param context
	 * @param @param press
	 * @param @param content
	 * @param @return
	 * @return Drawable
	 * @<b>作者</b> : huangszh
	 * @<b>创建时间</b> : 2013-12-10下午5:12:57
	 */
	@TargetApi(Build.VERSION_CODES.DONUT)
	public static Drawable getImageSpanDrawble(Context context, boolean press,
			String content) {
		LayoutInflater lf = (LayoutInflater) context
				.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
		TextView textView = (TextView) lf.inflate(R.layout.wblst_url_textview,
				null);
		if (content == null) {
			textView.setText("网页链接");
		} else {
			textView.setText(content);
		}

		if (press) {
			textView.setBackgroundResource(R.drawable.wblst_url_bg_press);
		} else {
			textView.setBackgroundResource(R.drawable.wblst_url_bg_normal);
		}
		// textView.setCompoundDrawablesWithIntrinsicBounds(0, 0,
		// R.drawable.android, 0);
		int spec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
		textView.measure(spec, spec);
		textView.layout(0, 0, textView.getMeasuredWidth(),
				textView.getMeasuredHeight());
		Bitmap b = Bitmap.createBitmap(textView.getWidth(),
				textView.getHeight(), Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas(b);
		canvas.translate(-textView.getScrollX(), -textView.getScrollY());
		textView.draw(canvas);
		textView.setDrawingCacheEnabled(true);
		Bitmap cacheBmp = textView.getDrawingCache();
		Bitmap viewBmp = cacheBmp.copy(Bitmap.Config.ARGB_8888, true);
		textView.destroyDrawingCache(); // destory drawable
		BitmapDrawable bmpDrawable = new BitmapDrawable(context.getResources(),
				viewBmp);
		bmpDrawable.setBounds(0, 0, bmpDrawable.getIntrinsicWidth(),
				bmpDrawable.getIntrinsicHeight());
		return bmpDrawable;
	}

	/**
	 * 
	 * @n<b>函数名称</b>     :isLocalTopic
	 * @brief
	 * @see
	 * @since    Ver 1.1
	 * @param  @param ti
	 * @param  @return
	 * @return boolean
	 * @<b>作者</b>          :  chenqian
	 * @<b>创建时间</b>      :  2014-3-24下午8:52:33
	 */
	public static boolean isLocalTopic(TopicInfo ti){
		if(null == ti || ti.tid <= 0){
			return true;
		}
		return false;
	}
	

	/**
	 * 格式化时间，简化成xx秒，xx分钟，xx天前
	 * 
	 * @param timestamp
	 *            unix时间戳 单位s
	 * @return 格式化后的字符串
	 */
	public static String format2HumanTime(Context ctx, long timestamp) {
		timestamp *= 1000L;
		long diffSecond = System.currentTimeMillis() - timestamp;
		diffSecond = diffSecond > 0 ? diffSecond : 0;

		final long MINUTE = 60 * 1000L;
		final long HOUR = MINUTE * 60;

		if (diffSecond < HOUR) {
			long minute = diffSecond / MINUTE;
			minute = minute == 0 ? 1 : minute;
			return minute
					+ ctx.getResources()
							.getString(R.string.minutes_before);
		}

		StringBuilder formateDate = new StringBuilder();
		String pattern = null;
		Calendar now = Calendar.getInstance();
		Calendar base = Calendar.getInstance();
		base.setTimeInMillis(timestamp);

		do {
			int diffYear = now.get(Calendar.YEAR) - base.get(Calendar.YEAR);
			if (diffYear > 0) {
				pattern = "yyyy-MM-dd HH:mm";
				break;
			}

			int diffDate = now.get(Calendar.DAY_OF_YEAR)
					- base.get(Calendar.DAY_OF_YEAR);
			if (diffDate == 0) {
				pattern = ctx.getResources()
						.getString(R.string.today_hm);
			} else if (diffDate == 1) {
				pattern = ctx.getResources()
						.getString(R.string.yesterday_hm);
			} else {
				pattern = "MM-dd HH:mm";
			}
		} while (false);

		FastDateFormat df = FastDateFormat.getInstance(pattern, null, null);
		formateDate.append(df.format(timestamp));
		return formateDate.toString();
	}

	/**
	 * 
	 * @n<b>函数名称</b>     :sendRefreshDataBroadCast
	 * @brief
	 * @see
	 * @since    Ver 1.1
	 * @param  @param ctx
	 * @param  @param tid
	 * @param  @param forwards转发
	 * @param  @param replys评论
	 * @param  @param praises赞
	 * @param  @param glances阅读数
	 * @param  @param isChangePraised是否需要改变被赞信息
	 * @param  @param errMsg错误信息
	 * @return void
	 * @<b>作者</b>          :  chenqian
	 * @<b>创建时间</b>      :  2014-3-11下午3:15:06
	 */
	public static void sendRefreshDataBroadCast(
			Context ctx, long tid, int forwards, int replys, int praises,int glances,boolean isChangePraised,String errMsg){
		if(null == ctx){
			return;
		}
		Intent broadMessage = new Intent(
				"refresh_data");
		broadMessage.putExtra("id", tid);
		broadMessage.putExtra("forwards", forwards);
		broadMessage.putExtra("replys", replys);
		broadMessage.putExtra("praises", praises);
		broadMessage.putExtra("glances", glances);
		broadMessage.putExtra("isChangePraised", isChangePraised);
		broadMessage.putExtra("errMsg", errMsg);
		LocalBroadcastManager.getInstance(ctx).sendBroadcast(
				broadMessage);
	}
	
}
